package com.mangguo.tingjian.account.rest;

import com.mangguo.tingjian.account.dto.MenuDto;
import com.mangguo.tingjian.common.Result;
import com.mangguo.tingjian.common.ResultStatus;
import com.mangguo.tingjian.account.entity.User;
import com.mangguo.tingjian.account.service.MenuService;
import com.mangguo.tingjian.account.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @GetMapping("/own-menu")
    public Result<List<MenuDto>> ownMenu(){
        Optional<User> user = userService.getUserWithAuthorities();
        if(!user.isPresent()){
            log.info("用户不存在");
            return Result.error(ResultStatus.USER_NOT_EXISTS);
        }
        return Result.success(menuService.findOwnMenu(user.get()));
    }

    @GetMapping("/all-menu")
    public Result<List<MenuDto>> allMenu(){
        Optional<User> user = userService.getUserWithAuthorities();
        if(!user.isPresent()){
            //TODO
            //只有技术部才有这个权限
        }
        return Result.success(menuService.findAllMenu());
    }

}
